package com.mcintyret.site.web.config;

import com.mcintyret.site.web.interceptor.JournalDbTransactionInterceptor;
import com.mcintyret.site.web.interceptor.UserContextInitializationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import javax.annotation.Resource;
import java.util.Properties;

/**
 * User: mcintyret2
 * Date: 28/03/2012
 */

@Configuration
@EnableWebMvc
@PropertySource("classpath:web-mvc.properties")
@ComponentScan(basePackages = "com.mcintyret.site", useDefaultFilters = false,
    includeFilters = @ComponentScan.Filter(Controller.class))
public class WebMvcContextConfig extends WebMvcConfigurerAdapter {

    private static final String TEMPLATE_EXCEPTION_HANDLER = "template_exception_handler";
    @Value("${freemarker.template_exception_handler}")
    private String templateExceptionHandler;

    @Resource
    private ApplicationContext applicationContext;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public FreeMarkerConfigurer freemarkerConfig() {
        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
        freeMarkerConfigurer.setTemplateLoaderPath("/");
        Properties freemarkerSettings = new Properties();
        freemarkerSettings.setProperty(TEMPLATE_EXCEPTION_HANDLER, templateExceptionHandler);
        freeMarkerConfigurer.setFreemarkerSettings(freemarkerSettings);
        return freeMarkerConfigurer;
    }

    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
        freeMarkerViewResolver.setCache(true);
        freeMarkerViewResolver.setPrefix("/");
        freeMarkerViewResolver.setSuffix(".ftl");
        freeMarkerViewResolver.setExposeSpringMacroHelpers(true);
        freeMarkerViewResolver.setOrder(1);
        freeMarkerViewResolver.setContentType("text/html;charset=UTF-8");
        return freeMarkerViewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(applicationContext.getBean(UserContextInitializationInterceptor.class));
        registry.addInterceptor(applicationContext.getBean(JournalDbTransactionInterceptor.class));
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}
